package com.yy.wga.admin.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.wga.module.admin.request.*;
import com.yy.wga.mp.common.aop.NotLogin;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.admin.service.pojo.AdminRoleDO;
import com.yy.wga.mp.module.admin.request.AdminRolePageRequest;
import com.yy.wga.mp.module.admin.request.AdminRoleSaveMenuRequest;
import com.yy.wga.mp.module.admin.request.AdminRoleSaveRequest;
import com.yy.wga.mp.module.admin.request.AdminRoleUpdateStatusRequest;
import com.yy.wga.mp.module.admin.response.AdminRoleInfoResponse;
import com.yy.wga.mp.module.admin.response.AdminRolePageResponse;
import com.yy.wga.mp.module.admin.service.AdminRoleService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 运营后台/角色信息
 *
 * @author guochao
 * @since 2024-11-06 22:56:22
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    /**
     * 服务对象
     */
    @Resource
    private AdminRoleService adminRoleService;

    @NotLogin
    @PostMapping(value = "/page")
    public Result<PageBean<AdminRolePageResponse>> page(@RequestBody @Valid AdminRolePageRequest request) {
        PageBean<AdminRolePageResponse> pageBean = adminRoleService.pageData(request);
        return Result.success(pageBean);
    }

    @NotLogin
    @PostMapping(value = "/list")
    public Result<List<AdminRoleInfoResponse>> list() {
        List<AdminRoleDO> adminRoleList = adminRoleService.list(new LambdaQueryWrapper<AdminRoleDO>()
                .eq(AdminRoleDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(AdminRoleDO::getStatus, 1));
        return Result.success(BeansUtils.mapList(adminRoleList, AdminRoleInfoResponse.class));
    }

    @PostMapping(value = "/updateStatus")
    public Result<Boolean> updateStatus(@RequestBody @Valid AdminRoleUpdateStatusRequest request) {
        adminRoleService.updateStatus(request);
        return Result.success(Boolean.TRUE);
    }

    @PostMapping(value = "/save")
    public Result<Boolean> save(@RequestBody @Valid AdminRoleSaveRequest request) {
        AdminRoleDO adminRole = BeansUtils.map(request, AdminRoleDO.class);
        adminRole.setAppKey(RequestUserHolder.getBaseRequest().getAppKey());
        adminRoleService.saveOrUpdate(adminRole);
        return Result.success(Boolean.TRUE);
    }

    @PostMapping(value = "/saveMenu")
    public Result<Boolean> saveMenu(@RequestBody @Valid AdminRoleSaveMenuRequest request) {
        adminRoleService.saveMenu(request);
        return Result.success(Boolean.TRUE);
    }

    @GetMapping(value = "/del")
    public Result<Boolean> del(@RequestParam("id") Long id) {
        adminRoleService.del(id);
        return Result.success(Boolean.TRUE);
    }
}
